﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using StockLib;
using StockLib.Indicator;
using StockUtil;

namespace StockControl
{
    public partial class StockKGraphControl : Control
    {
        const int MIN_DAYS = 5;

        StockKIndicator kIndicator = new StockKIndicator();
        List<StockIndicator> otherIndicators = new List<StockIndicator>();

        private int _indexStart;
        public int indexStart
        {
            get
            {
                return this._indexStart;
            }
            set
            {
                if (KData == null)
                    return;

                if (_indexStart <= 0 && value <= 0)
                    return;

                if (_indexStart >= _indexEnd - MIN_DAYS && value >= _indexEnd - MIN_DAYS)
                    return;

                if (value < 0)
                    _indexStart = 0;
                else if (value > _indexEnd - MIN_DAYS)
                    _indexStart = _indexEnd - MIN_DAYS;
                else
                    _indexStart = value;

                kIndicator.indexStart = _indexStart;
                foreach (StockIndicator indicator in otherIndicators)
                {
                    indicator.indexStart = _indexStart;
                }
            }
        }

        private int _indexEnd;
        public int indexEnd
        {
            get
            {
                return this._indexEnd;
            }
            set
            {
                if (KData == null)
                    return;

                if (_indexEnd >= KData.Count - 1 && value >= KData.Count - 1)
                    return;

                if (value > KData.Count - 1)
                    _indexEnd = KData.Count - 1;
                else
                    _indexEnd = value;

                kIndicator.indexEnd = _indexEnd;
                foreach (StockIndicator indicator in otherIndicators)
                {
                    indicator.indexEnd = _indexEnd;
                }
            }
        }

        private List<KData> _kData;
        public List<KData> KData
        {
            get
            {
                return this._kData;
            }
            set
            {
                this._kData = value;

                if (value != null)
                {
                    _indexEnd = _kData.Count - 1;
                    _indexStart = _indexEnd - 120;
                    if (_indexStart < 0)
                    {
                        _indexStart = 0;
                    }
                    kIndicator.indexEnd = _indexEnd;
                    kIndicator.indexStart = _indexStart;
                    foreach (StockIndicator indicator in otherIndicators)
                    {
                        indicator.indexStart = _indexStart;
                        indicator.indexEnd = _indexEnd;
                    }

                    kIndicator.kData = value;                    
                    kIndicator.Clear();
                    foreach (StockIndicator indicator in otherIndicators)
                    {
                        indicator.kData = value;
                        indicator.Clear();
                    }

                    Invalidate();
                }
            }
        }

        public Stock Stock { get; set; }

        private StockBitbltControl bitbltXAxis;
        private StockBitbltControl bitbltYAxis;
        private StockBitbltControl bitbltXCursor;
        private StockBitbltControl bitbltYCursor;

        public StockKGraphControl()
            : base()
        {
            InitializeComponent();

            this.bitbltXAxis = new StockBitbltControl();
            this.bitbltYAxis = new StockBitbltControl();
            this.bitbltXCursor = new StockBitbltControl();
            this.bitbltYCursor = new StockBitbltControl();
            this.Controls.Add(bitbltXAxis);
            this.Controls.Add(bitbltYAxis);
            this.Controls.Add(bitbltXCursor);
            this.Controls.Add(bitbltYCursor);

            this.Font = StockProfile.Fonts.GraphFont;

            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            //this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            kIndicator.otherIndicator = new StockMaIndicator();
            otherIndicators.Add(new StockVolIndicator());
            otherIndicators.Add(new StockMacdIndicator());
        }
    }
}
